package application.common.ui;

import application.common.util.RouteUtil;
import javafx.animation.Animation;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Layer {

	public MediaPlayer player;
	public Stage mainStage;
	public String message;
	public int messageType=0;
	public double width=400;
	public double height=250;

	public Layer(Stage mainStage,String message) {
		this.mainStage = mainStage;
		this.message = message;
	}
	
	public Layer(Stage mainStage,String message,int messageType) {
		this.mainStage = mainStage;
		this.message = message;
		this.messageType = messageType;
	}

	public void show(){
		final Stage stage = new Stage();	
		stage.initOwner(mainStage);
		stage.initModality(Modality.WINDOW_MODAL);
		
		stage.setWidth(width);
		stage.setHeight(height);
		stage.initStyle(StageStyle.UNDECORATED);
		
		Pane pane=new Pane();
		pane.setPrefSize(width, height);
		pane.setId("layer");
		
		ImageView iconView=new ImageView();
		iconView.setFitHeight(60.0);
		iconView.setFitWidth(60.0);
		iconView.setLayoutX(170.0);
		iconView.setLayoutY(30.0);
		if(messageType==999) {
			iconView.setImage(new Image(getClass().getResourceAsStream("/application/resources/warning.png")));
		}
		if(messageType==110) {
			iconView.setImage(new Image(getClass().getResourceAsStream("/application/resources/warning.png")));
		}
		else if(messageType==500) {
			iconView.setImage(new Image(getClass().getResourceAsStream("/application/resources/error.png")));
		}else {
			iconView.setImage(new Image(getClass().getResourceAsStream("/application/resources/info.png")));
		}
		
		Label messageLabel=new Label();
		messageLabel.setLayoutX(32.0);
		messageLabel.setLayoutY(108.0);
		messageLabel.setPrefSize(336.0, 50.0);
		messageLabel.setText(this.message);
		messageLabel.setId("layer-label");
		
		if(messageType==999) {
			play();
		}
		
		Button closeBtn=new Button();
		closeBtn.setLayoutX(164.0);
		closeBtn.setLayoutY(180.0);
		closeBtn.setText("确定");
		closeBtn.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				if(player!=null) {
					player.stop();
				}
				stage.close();
				RouteUtil.setTime(60);
			}
		});
		closeBtn.setPrefSize(72.0, 50);
		closeBtn.setId("layer-btn");
		
		
		pane.getChildren().add(iconView);
		pane.getChildren().add(messageLabel);
		pane.getChildren().add(closeBtn);

		Scene scene = new Scene(pane, width, height);
		scene.getStylesheets().add(getClass().getResource("/application/application.css").toExternalForm());
		stage.setScene(scene);
		stage.show();
	}
	
	public void play() {
		String url = getClass().getResource("/application/resources/warning.mp3").toString();
		Media media = new Media(url);
		player = new MediaPlayer(media);
		player.setAutoPlay(true);
		player.setCycleCount(Animation.INDEFINITE);
		player.play();
	}

}
